package com.platform.analysis.enums;

import com.platform.analysis.utils.FilterFunction;
import com.platform.comm.constants.EnumOption;
import com.platform.comm.enums.IEnumOption;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author liujizheng
 * @version 1.0
 * @ClassName FilterStrFunction
 * @Description 文本过滤条件
 * @date 2021/9/29 17:50
 */
@EnumOption(name = "filterStrFunction")
public enum FilterStrFunction implements IEnumOption<String>, FilterFunction {
    /**
     *
     */
    EQ("精确匹配", " ='%s' "),
    /**
     *
     */
    NE("不匹配", " !='%s' "),
    /**
     * %% 是对 % 的转义
     */
    CONTAIN("包含", " LIKE '%%%s%%' "),
    /**
     *
     */
    NOT_CONTAIN("不包含", " NOT LIKE '%%%s%%' "),
    /**
     *
     */
    START_WITH("开头是", " LIKE '%%%s' "),
    /**
     *
     */
    END_WITH("结尾是", " LIKE '%s%%' "),
    /**
     *
     */
    IS_NULL("为空", " IS NULL ", false),
    /**
     *
     */
    NOT_NULL("不为空", " IS NOT NULL ", false),
    /**
     *
     */
    IS_EMPTY("空文本", " = '' ",false),
    /**
     *
     */
    NOT_EMPTY("非空文本", " != '' ",false);

    String title;
    boolean needVal;
    String operation;
    FilterStrFunction(String title, String operation) {
        this.title = title;
        this.operation = operation;
        this.needVal = true;
    }
    FilterStrFunction(String title, String operation, boolean needVal) {
        this.title = title;
        this.operation = operation;
        this.needVal = needVal;
    }

    /**
     * 枚举项 “键”
     *
     * @return key 键
     */
    @Override
    public String key() {
        return name();
    }

    /**
     * 枚举项 “值”
     *
     * @return value 值
     */
    @Override
    public String value() {
        return title;
    }

    /**
     * 枚举options
     *
     * @return 枚举项组成的map
     */
    @Override
    public Map<String, String> enums() {
        return Arrays.stream(values()).collect(Collectors.toMap(IEnumOption::key, IEnumOption::value));
    }

    public static FilterStrFunction parse(String name) {
        Optional<String> optional = Arrays.stream(values()).map(FilterStrFunction::name).filter(fun -> fun.equals(name)).findAny();
        return optional.isPresent() ? valueOf(name) : null;
    }

    /**
     * Applies this function to the given argument.
     *
     * @param s the function argument
     * @return the function result
     */
    @Override
    public String apply(String s) {
        return String.format(operation, s);
    }

    /**
     * 是否需要输入参数
     *
     * @return true:需要参数 false:不需要参数
     */
    @Override
    public boolean needValue() {
        return needVal;
    }
}
